package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.wgts.wgts.control.eventbus.PayEvenBus;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerUpgradeShopKeeperFragmentComponent;
import com.wgts.wgts.dagger.module.UpgradeShopKeeperFragmentModule;
import com.wgts.wgts.mvp.presenter.IUpgradeShopKeeperFragmentPresenter;
import com.wgts.wgts.mvp.view.IUpgradeShopKeeperFragmentView;
import com.wgts.wgts.mvp.view.activity.goodsinfo.JDGoodinfoActivity;
import com.wgts.wgts.mvp.view.activity.wgtshome.WgtsProductAdapter;
import com.wgts.wgts.netmodle.resp.WgtsProductListEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.CustomImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class UpgradeShopKeeperFragment extends LazyBaseFragment implements IUpgradeShopKeeperFragmentView {

    Unbinder unbinder;


    @Inject
    IUpgradeShopKeeperFragmentPresenter presenter;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.linear_title)
    LinearLayout linearTitle;
    @BindView(R.id.img_head)
    ImageView imgHead;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.scroll_view)
    NestedScrollView scrollView;
    @BindView(R.id.frame_button)
    FrameLayout frameButton;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    private FragmentManager fragmentManager;


    private List<WgtsProductListEntity> entitys;

    UpdateDialog updateDialog;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //isVisibleToUser这个boolean值表示:该Fragment的UI 用户是否可见


//            final ArrayList<Fragment> fragments = new ArrayList<>();
//            fragments.add(InvitationRecordFragment.getInstance(0));
//            fragments.add(InvitationRecordFragment.getInstance(1));
//
//            viewPageReward.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
//                @Override
//                public Fragment getItem(int i) {
//                    return fragments.get(i);
//                }
//
//                @Override
//                public int getCount() {
//                    return fragments.size();
//                }
//            });
//
//            viewPageReward.setScanScroll(false);

    }

    @Override
    protected void loadData() {
        if (AppInfor.getInstance().getUserState() < 1) {
            imgHead.setImageResource(R.mipmap.vip_poster);
            presenter.productList("VIP");
        } else {
            imgHead.setImageResource(R.mipmap.super_vip_poster);
            presenter.productList("SVIP");
        }
        CustomImageView.getInstance().customImage(getContext(), imgHead, 1 / (double) 2);
    }

    @Override
    public void init() {
        DaggerUpgradeShopKeeperFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .upgradeShopKeeperFragmentModule(new UpgradeShopKeeperFragmentModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.upgrade_shop_keeper_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        EventBus.getDefault().register(this);
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    private void initView() {
        load();

        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setEnableRefresh(true);
    }

    @Override
    public void productResult(List<WgtsProductListEntity> entity) {
        refreshLayout.finishRefresh();
        this.entitys = entity;
        CustomLineaLayoutManager lineaLayoutManager = new CustomLineaLayoutManager(getContext());
        recycleView.setLayoutManager(lineaLayoutManager);
        WgtsProductAdapter vipGoodsAdapter = new WgtsProductAdapter(getContext(), entitys);
        recycleView.setAdapter(vipGoodsAdapter);

        vipGoodsAdapter.setOnClickListener(new WgtsProductAdapter.OnClickListener() {
            @Override
            public void onClick(View view, int i) {
                Intent intent = new Intent(getContext(), JDGoodinfoActivity.class);
                intent.putExtra(JDGoodinfoActivity.PRODUCT_ID, entitys.get(i).productId);
                //intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.VIP_PRODUCT);
                startActivity(intent);
            }
        });

//        fragmentManager = getChildFragmentManager();
//
//        if (fragmentManager.findFragmentById(R.id.frame_button) == null) {
//            fragmentManager.beginTransaction().add(R.id.frame_button, InvitationRecordFragment.getInstance(0)).commit();
//        }
    }

    @Override
    public void shopKeepPhone(String shopKeepPhone) {
        updateDialog = UpdateDialog.getInstance(shopKeepPhone);
        updateDialog.show(getFragmentManager(), "");
    }


    @Override
    public void onError(int status, String msg) {
        refreshLayout.finishRefresh();
    }

    private void load(){
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (AppInfor.getInstance().getUserState() < 1 ) {
                    imgHead.setImageResource(R.mipmap.vip_poster);
                    presenter.productList("VIP");
                } else {
                    imgHead.setImageResource(R.mipmap.super_vip_poster);
                    presenter.productList("SVIP");
                }
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(PayEvenBus event) {
        if (AppInfor.getInstance().getUserState() < 1) {
            imgHead.setImageResource(R.mipmap.vip_poster);
            presenter.productList("VIP");
        } else {
            imgHead.setImageResource(R.mipmap.super_vip_poster);
            presenter.productList("SVIP");
        }
    };

}
